package com.dong.boot.service;

import com.dong.boot.mapper.MenuMapper;
import com.dong.boot.mapper.MenuRoleMapper;
import com.dong.boot.mapper.RoleMapper;
import com.dong.boot.model.Menu;
import com.dong.boot.model.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleMenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;
    @Autowired
    RoleMapper roleMapper;

    public List<Menu> getMenusWithChildren() {
        return menuMapper.getAllMenus();
    }

    public List<Integer> getMidsByRid(Integer id) {
        return menuMapper.getMidsByRid(id);
    }

    @Transactional
    public Boolean updateRoleMenu(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if(mids==null || mids.length==0){
            return true;
        }
        menuRoleMapper.insertRecord(rid,mids);
        return true;
    }

    @Transactional
    public boolean deleteRole(Integer rid) {
        menuRoleMapper.deleteByRid(rid);
        roleMapper.deleteByPrimaryKey(rid);
        return true;
    }

    public boolean addRole(Role role) {
        roleMapper.insert(role);
        return true;
    }
}
